package com.jinkaimen.kqb.ui.activity.addmerchant;

import android.content.Intent;
import android.graphics.Color;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.ht.baselibs.bean.BaseGetBean;
import com.ht.baselibs.core.EventCenter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.UIUtils;
import com.igexin.sdk.PushManager;
import com.jinkaimen.kqb.R;
import com.jinkaimen.kqb.bean.AccountBean;
import com.jinkaimen.kqb.constant.Constants;
import com.jinkaimen.kqb.dao.AccountDao;
import com.jinkaimen.kqb.manager.ApiManager;
import com.jinkaimen.kqb.service.GetuiService;
import com.jinkaimen.kqb.service.UploadClientIdSevice;
import com.jinkaimen.kqb.thread.ValidateThread;
import com.jinkaimen.kqb.ui.activity.*;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import de.greenrobot.event.EventBus;
import retrofit2.Call;
import retrofit2.Response;

public class RegisterActivity extends BaseActivity {
    private TextView mTvRegisterPhone;
    private TextView mGetValidateBtn;
    private TextView mTvCodeNormal;
    private TextView mTvMyInviteCopeCode;
    private TextView mTvGoLogin;
    private AccountBean loginReturnBean;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setLeftBtnVisibility(View.GONE)
                .setTitleText("注册")//
                .setTitleTextColor(R.color.black)
                .setBackgroundColor(getResources().getColor(R.color.white));
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_register_add_merchant;
    }

    @Override
    protected void initChildView() {
        mTvRegisterPhone = (TextView) findViewById(R.id.tv_register_phone);
        mGetValidateBtn = (TextView) findViewById(R.id.tv_get_register_get_validate);
        mTvCodeNormal = (TextView) findViewById(R.id.tv_code_normal);
        mTvMyInviteCopeCode = (TextView) findViewById(R.id.tv_my_invite_cope_code);
        mTvGoLogin = (TextView) findViewById(R.id.tv_go_login);
    }

    @Override
    protected void initChildData() {

    }

    @Override
    protected void setChildViewListener() {
        mGetValidateBtn.setOnClickListener(this);
        findViewById(R.id.tv_activity_register).setOnClickListener(this);
        mTvGoLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_get_register_get_validate:
                String telNum = mTvRegisterPhone.getText().toString().trim();
                if (TextUtils.isEmpty(telNum)) {
                    UIUtils.showToast("请填写手机号");
                    return;
                }

                if (telNum.length() != 11) {
                    UIUtils.showToast("请填写正确的手机号");
                    return;
                }
                ApiManager.getApi().getValidate(
                        "HSS001005",
                        telNum,
                        Constants.OEM_NUM,
                        1
                ).enqueue(new NetCallBack<String>() {
                    @Override
                    protected void onSuccess(Call<String> call, Response<String> response) {
                        UIUtils.showToast("验证码发送成功");
                        new ValidateThread(mHandler).start();
                    }
                });
                break;
            case R.id.tv_activity_register:
                String tel = mTvRegisterPhone.getText().toString().trim();
                String code = mTvCodeNormal.getText().toString().trim();
                String inviteCode = mTvMyInviteCopeCode.getText().toString().trim();
                if (TextUtils.isEmpty(tel)) {
                    UIUtils.showToast("请填写手机号");
                    return;
                }

                if (tel.length() != 11) {
                    UIUtils.showToast("请填写正确的手机号");
                    return;
                }
                if (TextUtils.isEmpty(code)) {
                    UIUtils.showToast("请填写正确的手机号");
                    return;
                }
                if (TextUtils.isEmpty(inviteCode)) {
                    UIUtils.showToast("请填写邀请人注册手机号或邀请码");
                    return;
                }
                if (inviteCode.length() != 11 && inviteCode.length() != 6) {
                    UIUtils.showToast("邀请人注册手机号或邀请码填写错误");
                    return;
                }

                showLoadingDialogCanotCancle();
                ApiManager.getApi().stsrtRegister(
                        "HSS001006",
                        tel,
                        code,
                        inviteCode,
                        Constants.OEM_NUM
                ).enqueue(new NetCallBack<AccountBean>() {
                    @Override
                    protected void onSuccess(Call<AccountBean> call, Response<AccountBean> response) {
                        UIUtils.showToast("注册成功");
                        Intent downloadIntent = new Intent(RegisterActivity.this, UploadClientIdSevice.class);
                        RegisterActivity.this.startService(downloadIntent);
                        Intent downloadIntent1 = new Intent(RegisterActivity.this, GetuiService.class);
                        RegisterActivity.this.startService(downloadIntent1);

                        PushManager.getInstance().initialize(RegisterActivity.this, GetuiService.class); // 重新初始化sdk
                        EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_START_LOGIN));
                        loginReturnBean = response.body();
                        AccountDao.getInstance().addAccount(loginReturnBean);
                        String cid = PushManager.getInstance().getClientid(RegisterActivity.this);
                        if (!TextUtils.isEmpty(cid)) {
                            ApiManager.getApi().clientid(
                                    "HSS001002",
                                    cid
                            ).enqueue(new NetCallBack<BaseGetBean>() {
                                @Override
                                protected void onSuccess(Call<BaseGetBean> call, Response<BaseGetBean> response) {
                                    ActivityUtils.startActivityAndFinish(RegisterActivity.this, HomeActivity.class);
                                }

                                @Override
                                protected void hiddenProgress() {
                                    hideLodingDialog();
                                }

                            });
                        }

                    }

                    @Override
                    protected void hiddenProgress() {
                        hideLodingDialog();
                    }
                });
                break;
            case R.id.tv_go_login:
                ActivityUtils.finishActivity(this);
                break;
        }
    }

    /**
     * 匹配正则表达式
     *
     * @param str
     * @return
     */
    public static String stringFilter(String str) throws PatternSyntaxException {
        // 只允许字母、数字和汉字-
        //String   regEx  =  "[^a-zA-Z0-9-u4E00-u9FA5]";
        String regEx = "[^a-zA-Z0-9\u4e00-\u9fa5]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }


    @Override
    protected void handleMessage(BaseActivity context, Message msg) {
        switch (msg.what) {
            case 100:
                mGetValidateBtn.setText(msg.obj.toString());
                break;
            case 200:
                mGetValidateBtn.setTextColor(Color.parseColor("#6284dd"));
                mGetValidateBtn.setText("获取验证码");
                mGetValidateBtn.setEnabled(true);
                break;
        }
    }
}
